//
//  MyGradeViewController.m
//  MyGrade
//
//  Created by 田涛 on 2017/9/28.
//  Copyright © 2017年 Lemon. All rights reserved.
//

#import "MyGradeViewController.h"

#import "MyGradeTableHeaderView.h"
#import "MyGradeJYGCView.h"
#import "MyGradeDJTQView.h"
#import "MyGradeJYGCSectionHeaderView.h"
#import "MyGradeDJTQSectionHeaderView.h"

#import "MyGradeJYGCModel.h"
#import "MyGradeDJTQModel.h"

#import "GlobalHeader.h"

@interface MyGradeViewController () <UITableViewDelegate,UITableViewDataSource> {
    MyGradeTableHeaderView *_headerView;
    MyGradeJYGCView *_jygcView;
    MyGradeDJTQView *_djtqView;
    MyGradeJYGCSectionHeaderView *_jygcSectionView;
    MyGradeDJTQSectionHeaderView *_djtqSctionView;
}
/** 表格*/
@property (nonatomic, strong)  UITableView *tableView;
@end

@implementation MyGradeViewController

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
    //设置导航栏背景图片为一个空的image，这样就透明了
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    // 导航栏标题颜色
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:WidthScale_IOS7(16)],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
    [self getNetData];
}


/**
 *  初始化
 */
- (void)setup {
    self.navigationItem.title = @"我的等级";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"😆" style:UIBarButtonItemStylePlain target:self action:@selector(clickRightNavBtn)];
    
    [self.view addSubview:self.tableView];
    [self tableViewHeaderView];
}

/**
 *  获取数据
 */
- (void)getNetData {
    // 网络请求
    // 这里直接给 Model 赋死值
    
    /** --------- 表格头视图数据 ----------*/
    MyGradeHeaderViewModel *headerViewModel = [[MyGradeHeaderViewModel alloc] init];
    headerViewModel.headerImgUrl        = @"";
    headerViewModel.grade               = 6;
    headerViewModel.myCurrentExperience = 600;
    headerViewModel.defeatNum           = 50;
    headerViewModel.allExperience       = 900;
    headerViewModel.nextGrade           = 7;
    
    // --- 经验构成 ---
    MyGradeJYGCModel *jygcModel = [[MyGradeJYGCModel alloc] init];
    jygcModel.fbhdExperience = 220;
    jygcModel.hdzjExperience = 220;
    jygcModel.xqzjExperience = 220;
    jygcModel.qtjyExperience = 220;
    
    // --- 等级特权 ---
    MyGradeDJTQModel *djtqModel = [[MyGradeDJTQModel alloc] init];
    djtqModel.gradeDescription = @"LV.6:紫色等级标签";
    
    
    /** 虚拟网络加载延迟*/
    double delayInSeconds = 1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        
        // 给头视图赋值
        _headerView.model = headerViewModel;
        
        // 给经验构成 View 赋值
        _jygcView.model = jygcModel;
        
        // 给分区头视图赋值
        _djtqSctionView.model = djtqModel;
        
        // 给等级特权 View 赋值
        _djtqView.model = djtqModel;
        
        [self.tableView reloadData];
    });
}


/**
 *  表格头视图
 */
- (void)tableViewHeaderView {
    CGFloat width  = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height *0.34;
    
    MyGradeTableHeaderView *headerView = [[MyGradeTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    _headerView = headerView;
    
    self.tableView.tableHeaderView = _headerView;
}


#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"MyGradeID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
         [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    CGFloat width  = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    
    if (indexPath.section == 0) {
        _jygcView = [[MyGradeJYGCView alloc] initWithFrame:CGRectMake(0, 0, width, height *0.196)];
        [cell addSubview:_jygcView];
    } else {
        _djtqView = [[MyGradeDJTQView alloc] initWithFrame:CGRectMake(0, 0, width, height *0.354)];
        [cell addSubview:_djtqView];
    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return self.view.frame.size.height *0.196;
    } else {
        return self.view.frame.size.height *0.354;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return self.view.frame.size.height *0.055;
    } else {
        return self.view.frame.size.height *0.059;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CGFloat width  = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    if (section == 0) { // 经验构成
        _jygcSectionView = [[MyGradeJYGCSectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, width, height *0.060)];
        return _jygcSectionView;
    } else {
        _djtqSctionView = [[MyGradeDJTQSectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, width, height *0.059)];
        return _djtqSctionView;
    }
}

#pragma mark - Action
- (void)clickRightNavBtn {
    
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
        _tableView.delegate   = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}


@end

